var smokeAlarmData;
// 报警信息当前索引值
var alarmHandleNum = 0;
var smokeCombinedActionInfoArr = []

function smokeAlarmDataAjax() {
  $.ajax({
    url: '/alarm/notdispose',
    async: false,
    success: function (res) {
      smokeAlarmData = [];
      smokeAlarmData = res.data;
      console.log(smokeAlarmData, res)
      //			alarmHandleNum=smokeAlarmData.length-1;
      alarmHandleNum = 0;
      $('#historyNewsModal .historyNewsUnHandleNum').text(smokeAlarmData.length);
      $('.indexLeft .alarmSum').text(smokeAlarmData.length);
      if (smokeAlarmData.length != 0) {
        $('#smokeAlarm').show();
        $('.untreatedAlarmNum').show();
      } else {
        $('#smokeAlarm').hide();
        $('.untreatedAlarmNum').hide();
      }
    }
  });
  // var ret = getAlarmUndisposed();
  // ret.then(function (res) {
  //   console.log(res)
  //   smokeAlarmData = res.data;
  //   //			alarmHandleNum=smokeAlarmData.length-1;
  //   alarmHandleNum = 0;
  //   $('#historyNewsModal .historyNewsUnHandleNum').text(smokeAlarmData.length);
  //   $('.indexLeft .alarmSum').text(smokeAlarmData.length);
  //   if (smokeAlarmData.length != 0) {
  //     $('#smokeAlarm').show();
  //   } else {
  //     $('#smokeAlarm').hide();
  //   }
  // }).catch(function (err) {
  //   console.log(err)
  // });
}
// 根据报警信息条数 判断左右按钮是否亮起
function alarmpageTurningBtn() {
  console.log(alarmHandleNum, smokeAlarmData.length)
  if (alarmHandleNum == smokeAlarmData.length - 1) {
    $("#smokeAlarm .rightBtn").removeClass('selected')
  } else {
    $("#smokeAlarm .rightBtn").addClass('selected')
  }
  if (alarmHandleNum == 0) {
    $("#smokeAlarm .leftBtn").removeClass('selected')
  } else {
    $("#smokeAlarm .leftBtn").addClass('selected')
  }
}
// 模拟socket处理报警
function socketAlarmHandle(alarmId) {
  for (var i = 0; i < smokeAlarmData.length; i++) {
    if (alarmId == smokeAlarmData[i].alarmId) {
      smokeAlarmData.splice(i, 1);
      if (i == smokeAlarmData.length) {
        smokeAlarmRender(i - 1);
      } else {
        smokeAlarmRender(i);
      }
    }
  }
  if (smokeAlarmData.length == 0) {
    $('#smokeAlarm').hide();
  } else {
    $('#smokeAlarm').show();
  }
}
// 渲染报警信息
function smokeAlarmRender(num) {
  console.log(smokeAlarmData)
  if (smokeAlarmData.length != 0) {
    $('#smokeAlarm .alarmAddress span').text(smokeAlarmData[num].address);
    $('#smokeAlarm .alarmType').text(smokeAlarmData[num].alarmName);
    $('#smokeAlarm .alarmCode').text(smokeAlarmData[num].devCode);
    var time = smokeAlarmData[num].beginTime;
    console.log(time)
    time = moment(time).format("YYYY-MM-DD HH:mm:ss");
    $('#smokeAlarm .alarmTime').text(time);
    if (smokeAlarmData[num].linkMain1 && smokeAlarmData[num].linkPhone1) {
      $('#smokeAlarm .alarmPeople .people1 span').text('联系人1：' + smokeAlarmData[num].linkMain1 + '/' + smokeAlarmData[num].linkPhone1)
    } else if (smokeAlarmData[num].linkMain2 && smokeAlarmData[num].linkPhone2) {
      $('#smokeAlarm .alarmPeople .people2 span').text('联系人2：' + smokeAlarmData[num].linkMain2 + '/' + smokeAlarmData[num].linkPhone2)
    } else if (smokeAlarmData[num].linkMain3 && smokeAlarmData[num].linkPhone3) {
      $('#smokeAlarm .alarmPeople .people3 span').text('联系人3：' + smokeAlarmData[num].linkMain3 + '/' + smokeAlarmData[num].linkPhone3)
    }
  }
}
  // 处理和忽略封装ajax内容
  function handleIgnoreAjax(alarmId, disposeStatus, devCode) {

    // $.ajax({
    //   url: '/alarm/disposealarm?devCode=' + devCode + '&alarmId=' + alarmId + '&remark=null&alarmReasons=null&disposeStatus=' + disposeStatus,
    //   // async: false,
    //   success: function (res) {
    //     console.log(res)
    //   }
    // });
    var ret = getAlarmDisposealarm({
      devCode,
      alarmId,
      remark: 'null',
      alarmReasons: 'null',
      disposeStatus
    });
    ret.then(function (data) {
      console.log(data);
    }).catch(function (err) {
      console.log(err);
    });
  }
$(function () {
  // 旧版本报警框调用
  smokeAlarmDataAjax1();

  function smokeAlarmDataAjax1() {
    // $.ajax({
    //   url: '/alarm/notdispose',
    //   async: false,
    //   success: function (res) {
    //     smokeAlarmData = [];
    //     smokeAlarmData = res.data;
    //     //			alarmHandleNum=smokeAlarmData.length-1;
    //     alarmHandleNum = 0;
    //     $('#historyNewsModal .historyNewsUnHandleNum').text(smokeAlarmData.length);
    //     $('.indexLeft .alarmSum').text(smokeAlarmData.length);
    //     if (smokeAlarmData.length != 0) {
    //       $('#smokeAlarm').show();
    //     } else {
    //       $('#smokeAlarm').hide();
    //     }
    //   }
    // });
    var ret = getAlarmUndisposed();
    ret.then(function (res) {
      console.log(res)
      smokeAlarmData = [];
      smokeAlarmData = res.data;
      console.log(smokeAlarmData)
      //			alarmHandleNum=smokeAlarmData.length-1;
      alarmHandleNum = 0;
      $('#historyNewsModal .historyNewsUnHandleNum').text(smokeAlarmData.length);
      $('.indexLeft .alarmSum').text(smokeAlarmData.length);
      if (smokeAlarmData.length != 0) {
        $('#smokeAlarm').show();
      } else {
        $('#smokeAlarm').hide();
      }
      smokeAlarmRender(0);
      alarmpageTurningBtn();
    }).catch(function (err) {
      console.log(err);
    });
  }


  // 右翻页
  $('#smokeAlarm .rightBtn').on('click', function () {
    if (smokeAlarmData.length - 1 > alarmHandleNum) {
      alarmHandleNum += 1;
      smokeAlarmRender(alarmHandleNum);
      // smokeAlarmData.splice(alarmHandleNum,1)
    }
    alarmpageTurningBtn();
    console.log(smokeAlarmData);
  });
  // 左翻页
  $('#smokeAlarm .leftBtn').on('click', function () {
    if (alarmHandleNum > 0) {
      alarmHandleNum -= 1;
      smokeAlarmRender(alarmHandleNum);
    }
    // 翻页按钮判断
    alarmpageTurningBtn();
  });
  // 点击协同按钮
  $('#smokeAlarm .combinedActionBtn').on('click', function () {
    smokeCombinedActionInfoArr = smokeAlarmData[alarmHandleNum];
    console.log(smokeCombinedActionInfoArr);
    map.setZoom(16);
    // combinedActionMarker.push(BaiDuMap.combinedActionAddMarker0('1','left',200,'combinedActionMan','combinedActionAddress','combinedActionNum','combinedActionPhone', 'manager', 'telephone', 120.173955, 30.224156, 'no', 'position', 'orgName', 'combinedActionBuildingName'));
    // var l1=parseFloat(smokeCombinedActionInfoArr.latitude);
    // var l2=parseFloat(smokeCombinedActionInfoArr.longitude);
    // combinedActionMarker.push(BaiDuMap.combinedActionAddMarker0('1','left',200,'combinedActionMan','combinedActionAddress','combinedActionNum','combinedActionPhone', 'manager', 'telephone', l1,l2, smokeCombinedActionInfoArr.devCode, 'position', 'orgName', 'combinedActionBuildingName'));

    // BaiDuMap.panTo(smokeCombinedActionInfoArr.latitude,smokeCombinedActionInfoArr.longitude);
    // // 协同作战
    // combinedActionRender(smokeCombinedActionInfoArr.devCode);

    // console.log(smokeCombinedActionInfoArr);
  });
  $('.combinedActionBtn').on('click', function () {
    if (smokeCombinedActionInfoArr) {
      smokeCombinedActionInfoArr = smokeAlarmData[alarmHandleNum];
      console.log(smokeCombinedActionInfoArr.latitude, smokeCombinedActionInfoArr.longitude)
      // combinedActionMarker.push(BaiDuMap.combinedActionAddMarker0('1','left',200,'combinedActionMan','combinedActionAddress','combinedActionNum','combinedActionPhone', 'manager', 'telephone', 120.173955, 30.224156, 'no', 'position', 'orgName', 'combinedActionBuildingName'));
      var l1 = parseFloat(smokeCombinedActionInfoArr.latitude);
      var l2 = parseFloat(smokeCombinedActionInfoArr.longitude);
      setTimeout(function () {
        combinedActionMarker.push(BaiDuMap.combinedActionAddMarker0('1', 'left', 200, 'combinedActionMan', 'combinedActionAddress', 'combinedActionNum', 'combinedActionPhone', 'manager', 'telephone', l1, l2, smokeCombinedActionInfoArr.devCode, 'position', 'orgName', 'combinedActionBuildingName'));
      }, 0);

      BaiDuMap.panTo(smokeCombinedActionInfoArr.latitude, smokeCombinedActionInfoArr.longitude);
      // 协同作战
      combinedActionRender(smokeCombinedActionInfoArr.devCode);

      console.log(smokeCombinedActionInfoArr);
    }

  });
  $('.combinedActionTask-target').on('click', function () {

    smokeCombinedActionInfoArr = smokeAlarmData[alarmHandleNum];
  });

  // 火警报警处理
  $('#smokeAlarm .alarmHandleBtn').on('click', function () {
    var disposeStatus = 1;
    var alarmId = smokeAlarmData[alarmHandleNum].alarmId;
    var devCode = smokeAlarmData[alarmHandleNum].devCode;
    smokeAlarmData.splice(alarmHandleNum, 1)
    if (smokeAlarmData.length > alarmHandleNum) {
      smokeAlarmRender(alarmHandleNum);
    } else if (alarmHandleNum > 0) {
      alarmHandleNum -= 1;
      smokeAlarmRender(alarmHandleNum);
    } else if (smokeAlarmData.length == 0) {

      $('#smokeAlarm').hide();
      stopSound();
    }
    handleIgnoreAjax(alarmId, disposeStatus, devCode);
    alarmpageTurningBtn();
    smokeAlarmDataAjax();
  });
  
  // 点击任务单发送按钮结束火警报警
  // $('.taskListSendBtn').on('click', function () {
  //   var cooPointAroundNum = $("#taskListForm .cooPointAroundTabBigBox input[type=checkbox]:checked").length;
  //   var synergeticContentNum = $('#taskListForm .cooContentTabBox input[type=checkbox]:checked').length;
  //   if (cooPointAroundNum != 0 && synergeticContentNum != 0) {
  //     var disposeStatus = 1;
  //     var alarmId = smokeAlarmData[alarmHandleNum].alarmId;
  //     var devCode = smokeAlarmData[alarmHandleNum].devCode;
  //     smokeAlarmData.splice(alarmHandleNum, 1)
  //     if (smokeAlarmData.length > alarmHandleNum) {
  //       smokeAlarmRender(alarmHandleNum);
  //     } else if (alarmHandleNum > 0) {
  //       alarmHandleNum -= 1;
  //       smokeAlarmRender(alarmHandleNum);
  //     } else if (smokeAlarmData.length == 0) {

  //       $('#smokeAlarm').hide();
  //       stopSound();
  //     }
  //     handleIgnoreAjax(alarmId, disposeStatus, devCode);
  //     alarmpageTurningBtn();
  //     smokeAlarmDataAjax();
  //   }
  // });

  // 火警报警忽略
  $('#smokeAlarm .alarmIgnoreBtn').on('click', function () {
    var disposeStatus = 2;
    var alarmId = smokeAlarmData[alarmHandleNum].alarmId;
    var devCode = smokeAlarmData[alarmHandleNum].devCode;
    smokeAlarmData.splice(alarmHandleNum, 1)
    if (smokeAlarmData.length > alarmHandleNum) {
      smokeAlarmRender(alarmHandleNum);
    } else if (alarmHandleNum > 0) {
      alarmHandleNum -= 1;
      smokeAlarmRender(alarmHandleNum);
    } else if (smokeAlarmData.length == 0) {
      $('#smokeAlarm').hide();
      stopSound();
    }
    handleIgnoreAjax(alarmId, disposeStatus, devCode);
    alarmpageTurningBtn();
    smokeAlarmDataAjax();
  });




});